<template>
  <home-content title="在线听课人数">
    <chart-area
      v-if="onlineList.length > 0"
      :x-data="onlineDateList"
      :chart-data="onlineList"
      :timepicker="true"
      :toolbox="true"
      @reset-time="resetTime"
      tipName="在线人数"
    ></chart-area>
    <el-row class="onlineDetail">
      <el-col>
        <el-button type="primary" @click="$router.push('/online/detail')">查看详情</el-button>
      </el-col>
    </el-row>
    <div class="table_container">
      <el-row justify="center">
        <el-col :span="12">时间</el-col>
        <el-col :span="12">访问量</el-col>
      </el-row>
      <el-row v-for="(item,index) in onlineResultList" :key="index">
        <el-col class="table-item" :span="12">{{ item.day }}</el-col>
        <el-col class="table-item" :span="12">{{ item.onlineNum }}</el-col>
      </el-row>
    </div>
  </home-content>
</template>

<script>
import HomeContent from "@/components/HomeContent.vue";
import ChartArea from "@/components/ChartArea.vue";
import request from "@/utils/request";
import dayjs from "dayjs";
import { getOnlineData } from "@/utils/getChartsData";
export default {
  components: {
    "home-content": HomeContent,
    "chart-area": ChartArea
  },
  data() {
    return {
      startDate: null,
      endDate: null,
      onlineResultList: [],
      onlineDateList: [],
      onlineList: []
    };
  },
  created() {
    this.initList();
  },
  methods: {
    async initList(url) {
      this.startDate = dayjs()
        .subtract(10, "day")
        .format("YYYY-MM-DD");
      this.endDate = dayjs().format("YYYY-MM-DD");

      const result = await getOnlineData({
        startDate: this.startDate,
        endDate: this.endDate
      });
      this.onlineResultList = result.responseList;
      this.onlineDateList = result.xlist;
      this.onlineList = result.ylist;
    },
    resetTime(date) {
      this.startDate = dayjs(date[0]).format("YYYY-MM-DD");
      this.endDate = dayjs(date[1]).format("YYYY-MM-DD");
      this.initList();
    }
  }
};
</script>

<style lang="less" scoped>
.onlineDetail {
  margin-top: 30px;
  text-align: right;
}
.table_container {
  margin-top: 30px;
  border: 1px solid #e1e1e1;
  .el-row {
    border-top: 1px solid #eee;
  }
  .el-col {
    text-align: center;
    background-color: #f2f2f2;
    line-height: 58px;
    font-size: 16px;
    color: #666666ff;
    &:nth-child(1) {
      border-right: 1px solid #e0e0e0;
    }
  }
  .table-item {
    font-size: 14px;
    background-color: #fff;
  }
}
</style>